Monday, March 17, 2008

Code generation now uses templates

In Quameon, the derivatives for the Gaussian basis functions are computed with sympy and the python code is generated. Creating the code that surrounds the generated code is a bit of a tedious procedure - the syntax tree needs to be constructed by hand. But no more! The surrounding code can now written in python and a parser (written using pyparsing) will create the syntax tree for you.


Some details:

Identifiers surrounded by percent signs (eg %name%) get turned into a template node type for subsequent replacement.


Example template:


class %orb_name%:
def compute_value(self,alpha,v,r2):
x=v[0]
y=v[1]
z=v[2]
%value%
return val


The code replaces %orb_name% with the orbital name (s,px,py,px,etc) and %value% gets replaced with the expression for the value of the orbital (the methods for the derivatives not shown - see codegen/primitive_gaussian/deriv.py in SVN for the full template.)

No comments: